//
//  QSMyShareView.m
//  QSSmarkPark-iOS
//
//  Created by jxsoft on 2018/8/22.
//  Copyright © 2018年 QiSheng. All rights reserved.
//

#import "QSMyShareView.h"
#import <YYText.h>
#import "UIImage+QRCode.h"
@interface QSMyShareView ()

@property (nonatomic, strong) UIImageView *backgroundImageView;
@property (nonatomic, strong) UIImageView *logoImageView;
@property (nonatomic, strong) UIView *grayView;
@property (nonatomic, strong) UIImageView *QRcodeimageView;
@property (nonatomic, strong) YYLabel *QRcodeLabel;
@property (nonatomic, strong) UIButton *shareButton;
@property (nonatomic, strong) UILabel *bottomLabel;

@end

@implementation QSMyShareView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self loadUI];
        
        [UIImage createQRCodeImageForString:kShareAppURL size:kRealValue(135) ans:^(UIImage *image) {
            self.QRcodeimageView.image = image;
        }];
    }
    return self;
}

- (void)loadUI {
    [self.backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self);
        make.top.equalTo(self).offset(-kRealValue(8));
        make.height.equalTo(@(kScreenHeight - kNavgationBarHeight - kiPhoneXSafeAreaBottomMagin + kRealValue(8)));
    }];
    
    [self.logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(kRealValue(52));
        make.centerX.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(kRealValue(131), kRealValue(128)));
    }];
    
    [self.grayView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.logoImageView.mas_bottom).offset(kRealValue(71));
        make.centerX.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(kRealValue(150), kRealValue(150)));
    }];
    
    [self.QRcodeimageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.grayView);
        make.size.mas_equalTo(CGSizeMake(kRealValue(135), kRealValue(135)));
    }];
    
    [self.QRcodeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.grayView.mas_bottom).offset(kRealValue(5));
        make.centerX.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(kRealValue(150), kRealValue(54)));
    }];
    
    [self.shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.QRcodeLabel.mas_bottom).offset(kRealValue(21));
        make.size.mas_equalTo(CGSizeMake(kRealValue(152), kRealValue(34)));
    }];
    
    [self.bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(-kRealValue(38));
        make.centerX.equalTo(self);
        make.height.equalTo(@kRealValue(11));
    }];
}

#pragma mark - **************** Event Response
- (void)didClickShareButton {
    if (self.myShareViewDidClickShareButtonBlock) {
        self.myShareViewDidClickShareButtonBlock();
    }
}

#pragma mark - **************** Private Methods
- (NSMutableAttributedString*)getAttr:(NSString*)attributedString {
    NSMutableAttributedString * resultAttr = [[NSMutableAttributedString alloc] initWithString:attributedString];
    //对齐方式
    [resultAttr yy_setAlignment:NSTextAlignmentCenter range:NSMakeRange(0, attributedString.length)];
    //设置行间距
    resultAttr.yy_lineSpacing = kRealValue(10);
    //设置字体大小
    resultAttr.yy_font = [UIFont QSFontOfSize12];
    
    resultAttr.yy_color = [UIColor QSColorBlack6C6C6C];
    
    //可以设置某段字体的大小
    //[resultAttr yy_setFont:[UIFont boldSystemFontOfSize:CONTENT_FONT_SIZE] range:NSMakeRange(0, 3)];
    //设置字间距
    //resultAttr.yy_kern = [NSNumber numberWithFloat:1.0];
    
    return resultAttr;
}

#pragma mark - **************** Setter Getter
- (UIImageView *)backgroundImageView {
    if (!_backgroundImageView) {
        _backgroundImageView = [[UIImageView alloc] init];
//        _backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
//        _backgroundImageView.layer.masksToBounds = YES;
        [_backgroundImageView setImage:[UIImage imageNamed:@"img_fenxiang_plate_normal"]];
        [self addSubview:_backgroundImageView];
    }
    return _backgroundImageView;
}

- (UIImageView *)logoImageView {
    if (!_logoImageView) {
        _logoImageView = [[UIImageView alloc] init];
        [_logoImageView setImage:[UIImage imageNamed:@"share_logo"]];
        [self addSubview:_logoImageView];
    }
    return _logoImageView;
}

- (UIView *)grayView {
    if (!_grayView) {
        _grayView = [[UIView alloc] init];
        _grayView.backgroundColor = [UIColor QSColorGrayD8DFE4];
        [self addSubview:_grayView];
    }
    return _grayView;
}

- (UIImageView *)QRcodeimageView {
    if (!_QRcodeimageView) {
        _QRcodeimageView = [[UIImageView alloc] init];
        [self.grayView addSubview:_QRcodeimageView];
    }
    return _QRcodeimageView;
}

- (YYLabel *)QRcodeLabel {
    if (!_QRcodeLabel) {
        _QRcodeLabel = [[YYLabel alloc] init];
        _QRcodeLabel.numberOfLines = 2;
        NSMutableAttributedString  *titleAttr = [self getAttr:@"扫描二维码，让您的小伙 伴也加入吧"];
        _QRcodeLabel.attributedText = titleAttr;
        [_QRcodeLabel sizeToFit];
        [self addSubview:_QRcodeLabel];
    }
    return _QRcodeLabel;
}

- (UIButton *)shareButton {
    if (!_shareButton) {
        _shareButton = [[UIButton alloc] init];
        _shareButton.layer.borderWidth = kRealValue(1);
        _shareButton.layer.borderColor = [UIColor QSColorBlue3F7DEF].CGColor;
        _shareButton.layer.cornerRadius = kRealValue(17);
        [_shareButton setTitle:@"分享给好友" forState:UIControlStateNormal];
        [_shareButton setTitleColor:[UIColor QSColorBlue3F7DEF] forState:UIControlStateNormal];
        _shareButton.titleLabel.font = [UIFont QSFontOfSize15];
        [_shareButton addTarget:self action:@selector(didClickShareButton) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_shareButton];
    }
    return _shareButton;
}

- (UILabel *)bottomLabel {
    if (!_bottomLabel) {
        _bottomLabel = [[UILabel alloc] init];
        _bottomLabel.text = @"技术支持  齐圣科技";
        _bottomLabel.font = [UIFont QSFontOfSize11];
        _bottomLabel.textColor = [UIColor QSColorGrayCCCCCC];
        _bottomLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_bottomLabel];
    }
    return _bottomLabel;
}

@end
